package com.hsnn.medstgmini.trading.pur.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hsnn.medstgmini.model.IsLongtimePlan;
import com.hsnn.medstgmini.model.IsStatus;
import com.hsnn.medstgmini.trading.pur.dao.PurOrderInfoDao;
import com.hsnn.medstgmini.trading.pur.dao.PurPurPlanDao;
import com.hsnn.medstgmini.trading.pur.model.OrderIsRecepted;
import com.hsnn.medstgmini.trading.pur.model.OrderPackStatus;
import com.hsnn.medstgmini.trading.pur.model.PurOrderLogisticsOld;
import com.hsnn.medstgmini.trading.pur.model.form.DispatchInfo;
import com.hsnn.medstgmini.trading.pur.model.form.PurOrderInfoForm;
import com.hsnn.medstgmini.trading.pur.service.PurOrderInfoManager;
import com.hsnn.medstgmini.util.ConstantsUtil;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;
import com.hsnn.medstgmini.util.ParseNumber;

@Service
public class PurOrderInfoManagerImpl implements PurOrderInfoManager {

	@Autowired
	private PurOrderInfoDao purOrderInfoDao;
	
	@Resource(name = "sessionFactory")
	private SessionFactory factory;

	@Autowired
	private PurPurPlanDao purPurPlanDao;
	
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getOrdersDetailList(Pagination page) {
		page = purOrderInfoDao.getOrdersDetailList(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				map.put("orderStatus",map.get("orderStatus") == null ? "":OrderPackStatus.getName(Integer.valueOf(map.get("orderStatus").toString())));
			}
		}
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination getOrdersDetailListByProComp(Pagination page) {
		page = purOrderInfoDao.getOrdersDetailListByProComp(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				map.put("orderStatus",map.get("orderStatus") == null ? "":OrderPackStatus.getName(Integer.valueOf(map.get("orderStatus").toString())));
			}
		}
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination getOrdersDetailListByComp(Pagination page) {
		page = purOrderInfoDao.getOrdersDetailListByComp(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				map.put("submitDatetime",
						map.get("submitDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("submitDatetime").toString())));
				map.put("amount",
						map.get("amount") == null ? "0.00" : ParseNumber
								.toStrDecimal(ParseNumber.toDouble(map.get(
										"amount").toString())));

				map.put("count",
						map.get("count") == null ? "0" : map.get("count")
								.toString());
				map.put("isUrgent",
						map.get("isUrgent") == null ? "" : "1".equals(map.get("isUrgent").toString())?IsStatus.yes.getValue():IsStatus.no.getValue());
				map.put("orderStatus",OrderPackStatus.getName(Integer.parseInt(String.valueOf(map.get("orderStatus")))));
			}
		}
		return page;
	}

	@Override
	public Pagination getOrderInfoByHosp(Pagination page) {
		return purOrderInfoDao.getOrderInfoByHosp(page);
	}

	/**
	 * 
	 * @category 获取未完成订单信息列表数据（医疗机构）
	 * @author 韩守松
	 * @date 2015年12月11日 上午11:12
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getHospOderInfoDataList(Pagination page) {
		page = purOrderInfoDao.getHospOderInfoDataList(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				map.put("submitDatetime",
						map.get("submitDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("submitDatetime").toString())));
				map.put("amount",
						map.get("amount") == null ? "0.00" : ParseNumber
								.toStrDecimal(ParseNumber.toDouble(map.get(
										"amount").toString())));

				map.put("count",
						map.get("count") == null ? "0" : map.get("count")
								.toString());
				map.put("isUrgent",
						map.get("isUrgent") == null ? "" : "1".equals(map.get("isUrgent").toString())?IsStatus.yes.getValue():IsStatus.no.getValue());
				map.put("orderStatus",OrderPackStatus.getName(Integer.parseInt(String.valueOf(map.get("orderStatus")))));
						
			}
		}
		return page;
	}

	/**
	 * 
	 * @category 获取订单信息详情列表数据（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月10日 上午11:55:51
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getHospOderInfoDetailDataList(Pagination page) {
		page = purOrderInfoDao.getHospOderInfoDetailDataList(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		List<PurOrderInfoForm> flist = new ArrayList<PurOrderInfoForm>();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				PurOrderInfoForm form = new PurOrderInfoForm();
				form.setOrderCode(map.get("order_code") == null ? "" : map.get(
						"order_code").toString());
				form.setProPrice(map.get("pro_price") == null ? ""
						: ParseNumber.toStrDecimal(ParseNumber.toDouble(map
								.get("pro_price").toString())));
				form.setProCount(map.get("pro_count") == null ? "" : map.get(
						"pro_count").toString());
				form.setBuyPrice(map.get("buyprice") == null ? "" : ParseNumber
						.toStrDecimal(ParseNumber.toDouble(map.get("buyprice")
								.toString())));
				form.setDelCount(map.get("del_count") == null ? "" : map.get(
						"del_count").toString());
				form.setDelPrice(map.get("delPrice") == null ? "" : ParseNumber
						.toStrDecimal(ParseNumber.toDouble(map.get("delPrice")
								.toString())));
				form.setGoodsCode(map.get("goods_code") == null ? "" : map.get(
						"goods_code").toString());
				form.setDrugCode(map.get("drug_code") == null ? "" : map.get(
						"drug_code").toString());
				form.setDrugName(map.get("qualityspec_name") == null ? "" : map
						.get("qualityspec_name").toString());
				form.setFormName(map.get("form_name") == null ? "" : map.get(
						"form_name").toString());
				form.setSpecName(map.get("spec_name") == null ? "" : map.get(
						"spec_name").toString());
				form.setFactor(map.get("factor") == null ? "" : map.get(
						"factor").toString());
				form.setUnit(map.get("unit_name") == null ? "" : map.get(
						"unit_name").toString());
				form.setMaterialName(map.get("qualityspec_name") == null ? ""
						: map.get("qualityspec_name").toString());
				form.setCompName(map.get("prod_compname") == null ? "" : map
						.get("prod_compname").toString());
				form.setRecpCount(map.get("recp_count") == null ? "" : map.get(
						"recp_count").toString());
				form.setRecpPrice(map.get("recpPrice") == null ? ""
						: ParseNumber.toStrDecimal(ParseNumber.toDouble(map
								.get("recpPrice").toString())));
				form.setDelCompName(map.get("prod_compname") == null ? "" : map
						.get("prod_compname").toString());
				form.setCompId(map.get("prod_compcode") == null ? "" : map.get(
						"prod_compcode").toString());

				form.setProPriceLimit(map.get("pro_price_limit") == null ? ""
						: ParseNumber.toStrDecimal(ParseNumber.toDouble(map
								.get("pro_price_limit").toString())));
				if (null != map.get("is_longtime_plan")
						&& !"".equals(map.get("is_longtime_plan").toString()
								.trim())) {
					if (IsLongtimePlan.LongPlan.getKey() == Integer
							.valueOf((map.get("is_longtime_plan").toString()
									.trim()))) {
						form.setIsLongtimePlan(IsLongtimePlan.LongPlan
								.getValue());
					} else {
						form.setIsLongtimePlan(IsLongtimePlan.TimePlan
								.getValue());
					}
				}
				form.setOrderStatus(map.get("order_status") == null ? ""
						: ConstantsUtil.orderStatus(map.get("order_status")
								.toString()));
				flist.add(form);
			}
		}
		page.setRows(flist);
		return page;
	}

	/**
	 * 
	 * @category 获取物流信息（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月10日 下午2:12:40
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getOrderLogisticsData(Pagination page) {
		page = purOrderInfoDao.getOrderLogisticsData(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			Map<String, Object> map = list.get(0);
			String interflowDetail = map.get("interflowDetail") == null ? ""
					: map.get("interflowDetail").toString();
			String[] infos = interflowDetail.split(";");
			page.setRows(infos);
		}
		return page;
	}

	/**
	 * 
	 * @category 根据包号获取物流信息（医疗机构）
	 * @author 徐国鹏
	 * @date 2015年7月10日 下午2:12:40
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getOrderLogisticsDataOld(Pagination page) {
		page = purOrderInfoDao.getOrderLogisticsDataOld(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			Map<String, Object> map = list.get(0);
			String interflowDetail = map.get("interflowDetail") == null ? ""
					: map.get("interflowDetail").toString();
			String[] infos = interflowDetail.split(";");
			page.setRows(infos);
		}
		return page;
	}

	/**
	 * 
	 * @category 获取医疗机构确认收货页面数据（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月13日 下午2:20:42
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getConfirmRecpDataList(Pagination page) {
		page = purOrderInfoDao.getConfirmRecpDataList(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				map.put("submitDatetime",
						map.get("submitDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("submitDatetime").toString())));
				map.put("totalProPrice",
						map.get("totalProPrice") == null ? "" : ParseNumber
								.toStrDecimal(ParseNumber.toDouble(map.get(
										"totalProPrice").toString())));
				map.put("orderBegTime",
						map.get("orderBegTime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("orderBegTime").toString())));
				map.put("orderConfTime",
						map.get("orderConfTime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("orderConfTime").toString())));
				map.put("delDatetime",
						map.get("delDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("delDatetime").toString())));
				map.put("outDatetime",
						map.get("outDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("outDatetime").toString())));
				map.put("delMoney",
						map.get("delMoney") == null ? "" : ParseNumber
								.toStrDecimal(ParseNumber.toDouble(map.get(
										"delMoney").toString())));
				map.put("recpDatetime",
						map.get("recpDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("recpDatetime").toString())));
			}
		}
		return page;
	}

	/**
	 * @category 获取医疗机构确认收货订单明细页面数据（医疗机构）
	 * @author 韩守松
	 * @date 2015年11月29日
	 * @param @param page
	 * @param @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getConfirmRecpOrderDataList(Pagination page) {
		page = purOrderInfoDao.getConfirmRecpOrderDataList(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				map.put("approveCode", map.get("approve_code") == null ? ""
						: map.get("approve_code").toString());
				map.put("recpCount", map.get("del_count") == null ? "" : map
						.get("del_count").toString());
				map.put("invoiceNo", map.get("invoice_no") == null ? "" : map
						.get("invoice_no").toString());
				map.put("invoiceAmount",
						map.get("invoice_amount") == null ? ""
								: ParseNumber.toDecimal(map.get(
										"invoice_amount").toString()));
				map.put("invoiceDatetime",
						map.get("invoice_datetime") == null ? "" : ParseDate
								.parseShortFormat(ParseDate.fromShortFormat(map
										.get("invoice_datetime").toString())));
				map.put("remarks",
						map.get("remarks") == null ? "" : map.get("remarks")
								.toString());
				map.put("isRecepted",
						map.get("is_recepted") == null ? ""
								: Integer.valueOf(map.get("is_recepted")
										.toString()) == 0 ? OrderIsRecepted
										.getName(0)
										: Integer.valueOf(map
												.get("is_recepted").toString()) == 1 ? OrderIsRecepted
												.getName(1) : OrderIsRecepted
												.getName(2));
				map.put("delDetailId", map.get("del_detail_id") == null ? ""
						: map.get("del_detail_id").toString());

			}
		}
		return page;
	}

	/**
	 * 
	 * @category 获取确认订单详情页面（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月13日 下午2:50:39
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getConfirmRecpDetailDataList(Pagination page) {
		page = purOrderInfoDao.getConfirmRecpDetailDataList(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		List<PurOrderInfoForm> flist = new ArrayList<PurOrderInfoForm>();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				PurOrderInfoForm form = new PurOrderInfoForm();
				form.setOrderCode(map.get("order_code") == null ? "" : map.get(
						"order_code").toString());
				form.setProPrice(map.get("pro_price") == null ? ""
						: ParseNumber.toStrDecimal(ParseNumber.toDouble(map
								.get("pro_price").toString())));
				form.setProCount(map.get("pro_count") == null ? "" : map.get(
						"pro_count").toString());
				form.setBuyPrice(map.get("buyprice") == null ? "" : ParseNumber
						.toStrDecimal(ParseNumber.toDouble(map.get("buyprice")
								.toString())));
				form.setDelCount(map.get("del_count") == null ? "" : map.get(
						"del_count").toString());
				form.setDelPrice(map.get("delPrice") == null ? "" : ParseNumber
						.toStrDecimal(ParseNumber.toDouble(map.get("delPrice")
								.toString())));
				form.setGoodsCode(map.get("goods_code") == null ? "" : map.get(
						"goods_code").toString());
				form.setDrugCode(map.get("drug_code") == null ? "" : map.get(
						"drug_code").toString());
				form.setDrugName(map.get("qualityspec_name") == null ? "" : map
						.get("qualityspec_name").toString());
				form.setFormName(map.get("form_name") == null ? "" : map.get(
						"form_name").toString());
				form.setSpecName(map.get("spec_name") == null ? "" : map.get(
						"spec_name").toString());
				form.setFactor(map.get("factor") == null ? "" : map.get(
						"factor").toString());
				form.setUnit(map.get("unit_name") == null ? "" : map.get(
						"unit_name").toString());
				form.setMaterialName(map.get("material_name") == null ? ""
						: map.get("material_name").toString());
				form.setCompName(map.get("prod_compname") == null ? "" : map
						.get("prod_compname").toString());
				form.setRecpCount(map.get("recp_count") == null ? "" : map.get(
						"recp_count").toString());
				form.setRecpPrice(map.get("recpPrice") == null ? ""
						: ParseNumber.toStrDecimal(ParseNumber.toDouble(map
								.get("recpPrice").toString())));
				form.setOrderPackId(map.get("order_pack_id") == null ? "" : map
						.get("order_pack_id").toString());
				form.setCompId(map.get("prod_comp_id") == null ? "" : map.get(
						"prod_comp_id").toString());
				form.setDelCompName(map.get("prod_compcode") == null ? "" : map
						.get("prod_compcode").toString());
				if (null != map.get("is_longtime_plan")
						&& !"".equals(map.get("is_longtime_plan").toString()
								.trim())) {
					if (IsLongtimePlan.LongPlan.getKey() == Integer
							.valueOf((map.get("is_longtime_plan").toString()
									.trim()))) {
						form.setIsLongtimePlan(IsLongtimePlan.LongPlan
								.getValue());
					} else {
						form.setIsLongtimePlan(IsLongtimePlan.TimePlan
								.getValue());
					}

				}
				form.setIsRecepted(map.get("is_recepted") == null ? ""
						: ConstantsUtil.delOrderStatus(map.get("is_recepted")
								.toString()));
				form.setProPriceLimit(map.get("pro_price_limit") == null ? ""
						: ParseNumber.toStrDecimal(ParseNumber.toDouble(map
								.get("pro_price_limit").toString())));
				form.setDelDetailId(map.get("del_detail_id") == null ? "" : map
						.get("del_detail_id").toString());
				form.setApproveCode(map.get("approve_code") == null ? "" : map
						.get("approve_code").toString());
				form.setInvoiceAmount(map.get("invoice_amount") == null ? ""
						: map.get("invoice_amount").toString());
				form.setInvoiceDatetime(map.get("invoice_datetime") == null ? null
						: DateUtil.parseDate(map.get("invoice_datetime")
								.toString()));
				form.setDrugMedicine(map.get("drug_medicine") == null ? "" : map.get(
						"drug_medicine").toString());
				flist.add(form);
			}
		}
		page.setRows(flist);
		return page;
	}

	/**
	 * 
	 * @category 根据勾选的配送ID，获取所有的配送信息（医疗机构，为验证确认收货时到货数量不能大于配送数量）
	 * @author 邱磊
	 * @date 2015年7月13日 下午3:30:26
	 * @param list
	 * @return
	 */
	public Map<String, Integer> getDelInfoList(List<DispatchInfo> list) {
		List<Map<String, Object>> delList = purOrderInfoDao
				.getDelInfoList(list);
		Map<String, Integer> map = new HashMap<String, Integer>();
		if (null != delList && delList.size() > 0) {
			for (int i = 0; i < delList.size(); i++) {
				Map<String, Object> data = delList.get(i);
				map.put(data.get("del_detail_id") == null ? "" : data.get(
						"del_detail_id").toString(),
						data.get("del_count") == null ? null : ParseNumber
								.toInteger(data.get("del_count").toString()));
			}
		}
		return map;
	}

	/**
	 * 
	 * @category 确认收货按钮操作（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月13日 下午5:06:47
	 * @param list
	 */
	@Transactional
	public void confirmRecp(List<PurOrderInfoForm> list) {
		Session session = factory.getCurrentSession();
		Transaction tran = session.getTransaction();
		try {
			tran.begin();
			purOrderInfoDao.confirmRecp(list);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}
	}

	/**
	 * 
	 * @category 未到货功能操作（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月13日 下午5:35:25
	 * @param list
	 */
	@Transactional
	public void notArrived(List<PurOrderInfoForm> list) {
		Session session = factory.getCurrentSession();
		Transaction tran = session.getTransaction();
		try {
			tran.begin();
			purOrderInfoDao.notArrived(list);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		}
	}

	/**
	 * 
	 * @category 获取到货明细列表（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月13日 下午6:26:31
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getArrivedDrug(Pagination page) {
		page = purOrderInfoDao.getArrivedDrug(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		List<PurOrderInfoForm> flist = new ArrayList<PurOrderInfoForm>();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				PurOrderInfoForm form = new PurOrderInfoForm();
				form.setUserId(map.get("user_id") == null ? "" : map.get(
						"user_id").toString());
				form.setUserName(map.get("name") == null ? "" : map.get("name")
						.toString());
				form.setDrugCode(map.get("drug_code") == null ? "" : map.get(
						"drug_code").toString());
				form.setDrugName(map.get("qualityspec_name") == null ? "" : map
						.get("qualityspec_name").toString());
				form.setFormName(map.get("form_name") == null ? "" : map.get(
						"form_name").toString());
				form.setSpecName(map.get("spec_name") == null ? "" : map.get(
						"spec_name").toString());
				form.setFactor(map.get("factor") == null ? "" : map.get(
						"factor").toString());
				form.setUnit(map.get("unit_name") == null ? "" : map.get(
						"unit_name").toString());
				form.setMaterialName(map.get("material_name") == null ? ""
						: map.get("material_name").toString());
				form.setCompName(map.get("prod_compname") == null ? "" : map
						.get("prod_compname").toString());
				form.setDelCompId(map.get("del_comp_code") == null ? "" : map
						.get("del_comp_code").toString());
				form.setDelCompName(map.get("delCompName") == null ? "" : map
						.get("delCompName").toString());
				form.setReturnCount(map.get("return_count") == null ? "" : map
						.get("return_count").toString());
				form.setApproveCode(map.get("approve_code") == null ? "" : map
						.get("approve_code").toString());
				form.setProPrice(map.get("pro_price") == null ? ""
						: ParseNumber.toStrDecimal(ParseNumber.toDouble(map
								.get("pro_price").toString())));
				form.setTotalAmount(map.get("total_amount") == null ? "" : map
						.get("total_amount").toString());
				form.setCompId(map.get("prod_compcode") == null ? "" : map.get(
						"prod_compcode").toString());
				form.setProjName(map.get("projName") == null ? "" : map.get(
						"projName").toString());
				form.setCredential(map.get("credential") == null ? "" : map.get(
						"credential").toString());
				form.setDrugMedicine(map.get("drugMedicine") == null ? "" : map.get(
						"drugMedicine").toString());
				flist.add(form);
			}
		}
		page.setRows(flist);
		return page;
	}

	/**
	 * 
	 * @category 整包确认收货（医疗机构操作，所有到货数量就是配送数量）
	 * @author 邱磊
	 * @date 2015年7月14日 上午9:38:00
	 * @param page
	 */
	@Transactional
	public void confirmByPack(Pagination page) {
		purOrderInfoDao.confirmByPack(page);
	}

	/**
	 * 
	 * @category 整包未到货操作（医疗机构）
	 * @author 邱磊
	 * @date 2015年7月14日 上午10:16:35
	 * @param page
	 */
	@Transactional
	public void noArrivedByPack(Pagination page) {
		purOrderInfoDao.noArrivedByPack(page);
	}

	/**
	 * 
	 * @category 根据订单明细ID查询物流信息
	 * @author 邱磊
	 * @date 2015年7月14日 上午10:51:11
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination toDeliverDetailById(Pagination page) {
		page = purOrderInfoDao.toDeliverDetailById(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			Map<String, Object> map = list.get(0);
			String interflowDetail = map.get("interflowDetail") == null ? ""
					: map.get("interflowDetail").toString();
			String[] infos = interflowDetail.split(";");
			page.setRows(infos);
		}
		return page;
	}

	/**
	 * 
	 * @category 根据订单明细ID查询物流信息
	 * @author 徐国鹏 // * @date 2015年8月7日 下午14:51:11
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination toDeliverDetailByIdOld(Pagination page) {
		page = purOrderInfoDao.toDeliverDetailByIdOld(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			Map<String, Object> map = list.get(0);
			String interflowDetail = map.get("interflowDetail") == null ? ""
					: map.get("interflowDetail").toString();
			String[] infos = interflowDetail.split(";");
			page.setRows(infos);
		}
		return page;
	}

	/**
	 * 
	 * @category 获取未评价订单列表
	 * @author 邱磊
	 * @date 2015年7月19日 下午1:42:40
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getUnReviewsDataList(Pagination page) {
		page = purOrderInfoDao.getUnReviewsDataList(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				map.put("orderBegTime",
						map.get("orderBegTime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("orderBegTime").toString())));
				map.put("orderConfEndTime",
						map.get("orderConfEndTime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("orderConfEndTime").toString())));
				map.put("submitDatetime",
						map.get("submitDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("submitDatetime").toString())));
				map.put("recpDatetime",
						map.get("recpDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("recpDatetime").toString())));
			}
		}
		page.setRows(list);
		return page;
	}

	/**
	 * 
	 * @category 根据物流信息ID查询物流历史信息
	 * @author 邱磊
	 * @date 2015年7月19日 下午8:43:36
	 * @param interflowId
	 * @return
	 */
	public PurOrderInfoForm getLogisticsInfo(String interflowId) {
		PurOrderLogisticsOld data = purOrderInfoDao
				.getLogisticsInfo(interflowId);
		PurOrderInfoForm info = new PurOrderInfoForm();
		BeanUtils.copyProperties(data, info);
		info.setOrderPackId(data.getInterflowId());
		// info.setInterflowDetail(interflowId);
		// info.setInterflowDetailList(info.getInterflowDetail().split(";"));
		info.setDelDatetime(ParseDate.parseFullFormat(data.getDelDatetime()));
		info.setDelDatetime(info.getDelDatetime() == null ? ""
				: ParseDate.parseFullFormat(ParseDate.fromFullFormat(info
						.getDelDatetime())));
		info.setRecpDatetime(info.getRecpDatetime() == null ? "" : ParseDate
				.parseFullFormat(ParseDate.fromFullFormat(info
						.getRecpDatetime())));
		return info;
	}

	/**
	 * 
	 * @category 保存订单评价信息
	 * @author 邱磊
	 * @date 2015年7月19日 下午8:49:41
	 * @param page
	 */
	public void saveReviews(Pagination page) {
		purOrderInfoDao.saveReviews(page);
	}

	/**
	 * 
	 * @category 查询已评价列表
	 * @author 邱磊
	 * @date 2015年7月20日 下午5:13:09
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getReviewsDataList(Pagination page) {
		page = purOrderInfoDao.getReviewsDataList(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				map.put("delDatetime",
						map.get("delDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("delDatetime").toString())));
				map.put("recpDatetime",
						map.get("recpDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("recpDatetime").toString())));
				map.put("gradeDatetime",
						map.get("gradeDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("gradeDatetime").toString())));
				map.put("submitDatetime",
						map.get("submitDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("submitDatetime").toString())));
			}
		}
		page.setRows(list);
		return page;
	}

	// //////////////////////////
	/**
	 * 
	 * @category 查询已评价详情页面
	 * @author 徐国鹏
	 * @date 2015年8月4日 下午5:13:09
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getReviewsDataListDetail(Pagination page) {
		page = purOrderInfoDao.getReviewsDataListDetail(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				map.put("delDatetime",
						map.get("delDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("delDatetime").toString())));
				map.put("recpDatetime",
						map.get("recpDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("recpDatetime").toString())));
				map.put("gradeDatetime",
						map.get("gradeDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("gradeDatetime").toString())));
				map.put("orderBegTime",
						map.get("orderBegTime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("orderBegTime").toString())));
				map.put("submitDatetime",
						map.get("submitDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("submitDatetime").toString())));
			}
		}
		page.setRows(list);
		return page;
	}

	/**
	 * @category 查看完成订单信息页面（医疗机构端）
	 * @Auther 韩守松
	 * @date 2015年11月30日
	 * @param page
	 * @return Pagination
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getHospOderInfoOld(Pagination page) {
		page = purOrderInfoDao.getHospOderInfoOld(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page.getRows();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				map.put("submitDatetime",
						map.get("submitDatetime") == null ? "" : ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("submitDatetime").toString())));
				map.put("orderStatus",OrderPackStatus.getName(Integer.parseInt(String.valueOf(map.get("orderStatus")))));
			}
		}
		return page;
	}

	/**
	 * @category 获取历史订单详情页面（医疗机构端）
	 * @Auther 徐国鹏
	 * @date 2015年7月30日 上午9:30:00
	 * @param page
	 * @return Pagination
	 */
	@SuppressWarnings("unchecked")
	public Pagination getHospOderOldDetail(Pagination page) {
		page = purOrderInfoDao.getHospOderOldDetail(page);
		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();
		List<PurOrderInfoForm> flist = new ArrayList<PurOrderInfoForm>();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> map = list.get(i);
				PurOrderInfoForm form = new PurOrderInfoForm();
				form.setOrderCode(map.get("order_code") == null ? "" : map.get(
						"order_code").toString());
				form.setProPrice(map.get("pro_price") == null ? ""
						: ParseNumber.toStrDecimal(ParseNumber.toDouble(map
								.get("pro_price").toString())));
				form.setProCount(map.get("pro_count") == null ? "" : map.get(
						"pro_count").toString());
				form.setBuyPrice(map.get("buyprice") == null ? "" : ParseNumber
						.toStrDecimal(ParseNumber.toDouble(map.get("buyprice")
								.toString())));
				form.setDelCount(map.get("del_count") == null ? "" : map.get(
						"del_count").toString());
				form.setDelPrice(map.get("delPrice") == null ? "" : ParseNumber
						.toStrDecimal(Double.valueOf(map.get("delPrice")
								.toString())));
				form.setGoodsCode(map.get("goods_code") == null ? "" : map.get(
						"goods_code").toString());
				form.setDrugCode(map.get("drug_code") == null ? "" : map.get(
						"drug_code").toString());
				form.setDrugName(map.get("drug_name") == null ? "" : map.get(
						"drug_name").toString());
				form.setFormName(map.get("form_name") == null ? "" : map.get(
						"form_name").toString());
				form.setSpecName(map.get("spec_name") == null ? "" : map.get(
						"spec_name").toString());
				form.setFactor(map.get("factor") == null ? "" : map.get(
						"factor").toString());
				form.setUnit(map.get("unit") == null ? "" : map.get("unit")
						.toString());
				form.setMaterialName(map.get("material_name") == null ? ""
						: map.get("material_name").toString());
				form.setCompName(map.get("comp_name") == null ? "" : map.get(
						"comp_name").toString());
				form.setRecpCount(map.get("recp_count") == null ? "" : map.get(
						"recp_count").toString());
				form.setRecpPrice(map.get("recpPrice") == null ? "" : map.get(
						"recpPrice").toString());
				form.setDrugId(map.get("drug_id") == null ? "" : map.get(
						"drug_id").toString());
				form.setDelCompName(map.get("delCompName") == null ? "" : map
						.get("delCompName").toString());
				form.setDelCompId(map.get("del_comp_id") == null ? "" : map
						.get("del_comp_id").toString());
				form.setCompId(map.get("prod_comp_id") == null ? "" : map.get(
						"prod_comp_id").toString());
				form.setIsLongtimePlan("非备案药品");
				if (null != map.get("bak_drug_flag")
						&& !"".equals(map.get("bak_drug_flag").toString()
								.trim())) {
					if ("1".equals(map.get("bak_drug_flag").toString().trim())) {
						if (null != map.get("is_longtime_plan")
								&& !"".equals(map.get("is_longtime_plan")
										.toString().trim())) {
							if ("0".equals(map.get("is_longtime_plan")
									.toString().trim())) {
								form.setIsLongtimePlan("临时备案药品");
							} else if ("1".equals(map.get("is_longtime_plan")
									.toString().trim())) {
								form.setIsLongtimePlan("长期备案药品");
							}
						}
					}
				}
				form.setOrderStatus(map.get("order_status") == null ? ""
						: ConstantsUtil.orderStatus(map.get("order_status")
								.toString()));
				form.setArchiveType(map.get("dic_name") == null ? "" : map.get(
						"dic_name").toString());
				form.setProPriceLimit(map.get("pro_price_limit") == null ? ""
						: ParseNumber.toStrDecimal(Double.valueOf(map.get(
								"pro_price_limit").toString())));
				flist.add(form);
			}
		}
		page.setRows(flist);
		return page;
	}

	/**
	 * 
	 */
	public PurOrderLogisticsOld getAoginfoById(String interflowId) {
		PurOrderLogisticsOld data = purOrderInfoDao.getAoginfoById(interflowId);
		return data;
	}

	/**
	 * @category 根据物流id查询
	 * @author wangbing
	 * @date 2015年11月25日
	 * @param
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getPurOrderlogisticsById(String id) {
		List<Map<String, Object>> list = (List<Map<String, Object>>) purOrderInfoDao
				.getPurOrderlogisticsById(id);
		for (Map<String, Object> data : list) {
			data.put("midDatetime",
					DateUtil.dateTime((Timestamp) data.get("midDatetime")));
		}
		return list;
	}

	/**
	 * @category 根据物流id查询各个时间点
	 * @author wangbing
	 * @date 2015年11月25日
	 * @param
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getTheTimesById(String id) {
		List<Object> list = (List<Object>) purOrderInfoDao.getTheTimesById(id);
		Map<String, Object> data = new HashMap<String, Object>();
		if (list != null && list.size() != 0) {
			data.put("delStatus", "ready");
			data.put("recpStatus", "ready");
		} else {
			data.put("delStatus", "doing");
			data.put("recpStatus", "wait");
		}
		data.put("joinStatus", "ready");
		data.put("subStatus", "ready");
		data.put("confirmStatus", "ready");
		data.put("interflowId", id);
		return data;
	}

	/**
	 * @category 根据物流id查询各个订单商品
	 * @author wangbing
	 * @date 2015年11月25日
	 * @param
	 */
	@Override
	public List<?> getGoodInfoById(String id) {
		return purOrderInfoDao.getGoodInfoById(id);
	}

	@Transactional
	public void auditHospOderInfo(Pagination page) {
		purOrderInfoDao.auditHospOderInfo(page);
		if(StringUtils.isNotBlank(page.getConditions().get("allAmount")) && StringUtils.isNotBlank(page.getConditions().get("baseAllAmount"))){
			//更新医院备案采购交易金额
			purPurPlanDao.updateHisRecordAmount(page);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination getNotAuditHospOderList(Pagination page) {
		page = purOrderInfoDao.getNotAuditHospOderList(page);
		List<Map<String,Object>> list =(List<Map<String, Object>>) page.getRows();
		Map<String,Object> map = new HashMap<String, Object>();
		if(list.size() > 0){
			for(int i =0 ;i < list.size();i++){
				map = list.get(i);
				map.put("submitDatetime", map.get("submitDatetime")==null?"":ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("submitDatetime").toString())));
		 }
		}
		return page;
		
	}

	@Override
	public Object checkAuditHospOder(Pagination page) {
		return purOrderInfoDao.checkAuditHospOder(page);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination getAuditHospOderResultList(Pagination page) {
		page = purOrderInfoDao.getAuditHospOderResultList(page);
		List<Map<String,Object>> list =(List<Map<String, Object>>) page.getRows();
		Map<String,Object> map = new HashMap<String, Object>();
		if(list.size() > 0){
			for(int i =0 ;i < list.size();i++){
				map = list.get(i);
				map.put("submitDatetime", map.get("submitDatetime")==null?"":ParseDate
								.parseFullFormat(ParseDate.fromFullFormat(map
										.get("submitDatetime").toString())));
				map.put("orderStatus", map.get("orderStatus")==null?"其他":OrderPackStatus.getName(Integer.valueOf(map.get("orderStatus").toString())));
		 }
		}
		return page;
	}
	
	@Override
	public Pagination checkHisRecordAmount(Pagination page) {
		//获取历史采购金额
		Object obj =    purPurPlanDao.getHisRecordAmount(page).getRows();
		//查询勾选即将审核通过的订单包号详情信息
		List<HashMap<String, Object>> list =  purOrderInfoDao.getOrderPackAmountList(page);
		if(list.size()<=0){
			return null;
		}
		page.setRows(list);
		page.setForm(obj);
		return page;
	}
}
